﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;


namespace zUfDsl.Common
{
    public static class HandleObject
    {
        private static Dictionary<Type, List<PropertyInfo>> typedic = new Dictionary<Type, List<PropertyInfo>>();

        public static List<PropertyInfo> GetTypePropertyInfo(Type t)
        {
            if (!typedic.ContainsKey(t))
            {
                typedic.Add(t, t.GetProperties().ToList());
            }
            return typedic[t];
        }

        private static Dictionary<Type, List<FieldInfo>> filedic = new Dictionary<Type, List<FieldInfo>>();

        public static List<FieldInfo> GetTypeFieldInfo(Type t)
        {
            if (!filedic.ContainsKey(t))
            {
                filedic.Add(t, t.GetFields().ToList());
            }
            return filedic[t];
        }


        public static T ChangeObject<T, V>(V ori)
        {
            var oriinfo = GetTypePropertyInfo(typeof(V));
            var newinfo = GetTypePropertyInfo(typeof(T));
            T t = Activator.CreateInstance<T>(); //构造新实例
            foreach (PropertyInfo sp in oriinfo)//获得类型的属性字段  
            {
                foreach (PropertyInfo dp in newinfo)
                {
                    if (dp.Name == sp.Name && dp.PropertyType == sp.PropertyType && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同  
                    {
                        dp.SetValue(t, sp.GetValue(ori, null), null);//获得s对象属性的值复制给d对象的属性  
                    }
                }
            }
            return t;
        }

        public static List<T> ChangeList<T, V>(List<V> lori)
        {
            var list = new List<T>();
            for (int i = 0; i < lori.Count; i++)
            {
                var t = ChangeObject<T>(lori[i]);
                list.Add(t);
            }
            return list;
        }
        public static void ChangeList<T, V>(List<V> lori, out List<T> lnew)
        {
            lnew = new List<T>();
            for (int i = 0; i < lori.Count; i++)
            {
                var t = ChangeObject<T>(lori[i]);
                lnew.Add(t);
            }
        }

        public static List<T> ChangeListSame<T>(object[] lori)
        {
            var lnew = new List<T>();
            for (int i = 0; i < lori.Count(); i++)
            {
                var t = lori[i];
                lnew.Add((T)t);
            }
            return lnew;
        }
        public static List<T> ChangeList<T>(object[] lori)
        {
            var lnew = new List<T>();
            for (int i = 0; i < lori.Count(); i++)
            {
                var t = ChangeObject<T>(lori[i]);
                lnew.Add(t);
            }
            return lnew;
        }

        public static T ChangeObject<T>(object ori)
        {
            var oriinfo = GetTypePropertyInfo(ori.GetType());
            var newinfo = GetTypePropertyInfo(typeof(T));
            T t = Activator.CreateInstance<T>(); //构造新实例
            foreach (PropertyInfo sp in oriinfo)//获得类型的属性字段  
            {
                foreach (PropertyInfo dp in newinfo)
                {
                    if (dp.Name == sp.Name && dp.Name != "Error" && dp.Name != "Item")//判断属性名是否相同  
                    {
                        if (dp.PropertyType == sp.PropertyType)
                        {
                            dp.SetValue(t, sp.GetValue(ori, null), null);//获得s对象属性的值复制给d对象的属性  
                        }
                        continue;
                    }
                }
            }
            return t;
        }

        #region 获取级次编码位数的长度
        /// <summary>
        /// 获取级次编码位数的长度
        /// </summary>
        public static int GetGradeLengthThis(string GradeDef, int igrade)
        {
            var ilength = GradeDef.Substring(igrade - 1, 1).ObjToInt();
            return ilength;
        }
        #endregion 

        #region 获取级次编码位数的长度
        /// <summary>
        /// 获取级次编码位数的长度
        /// </summary>
        public static int GetGradeLengthAll(string GradeDef, int igrade)
        {
            var ilength = 0;
            for (int i = 0; i < igrade; i++)
            {
                var tmpgradelen = GradeDef.Substring(i, 1);
                ilength += tmpgradelen.ObjToInt();
            }
            return ilength;
        }
        #endregion


        #region 判断编码是否符合级次规则
        /// <summary>
        /// 判断编码是否符合级次规则
        /// </summary>
        /// <param name="cgradedef"></param>
        /// <param name="ccode"></param>
        /// <param name="parentcode"></param>
        /// <returns></returns>
        public static string JudGrade(string cgradedef, string ccode, out string parentcode)
        {
            var err = "";
            int igradeparent = 0;
            int tgrade = 0;
            parentcode = "";
            if (cgradedef.Length == 0)
            {
                err += string.Format("请设置分类编码级次；");
                return err;
            }
            for (int i = 0; i < cgradedef.Length; i++)
            {
                igradeparent = tgrade;
                var igrade = Convert.ToInt32(cgradedef.Substring(i, 1));
                tgrade += igrade;
                if (ccode.Length <= tgrade) { break; }
            }
            if (tgrade != ccode.Length)
            { err += string.Format("编码[{0}]不符合编码规则[{1}]；", ccode, cgradedef); }
            if (igradeparent != 0)
            {
                parentcode = ccode.Substring(0, igradeparent);
            }
            return err;
        }
        #endregion 
    }
}
